package com.lc.luzhengtang.fragment;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lc.luzhengtang.R;
import com.lc.luzhengtang.activity.NoticeActivity;
import com.lc.luzhengtang.adapter.IndexAdapter;
import com.lc.luzhengtang.banner.BannerLayout;
import com.lc.luzhengtang.bean.IndexBean;
import com.lc.luzhengtang.conn.Conn;
import com.lc.luzhengtang.conn.ConnIndex;
import com.lc.luzhengtang.divider.SimpleDividerItemDecoration;
 ;
import com.lc.luzhengtang.layoutmanager.FullyGridLayoutManager;
import com.lc.luzhengtang.marqueue.MarqueeView;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/5/3.
 */

public class AllFragment extends BaseFragment {
    @BindView(R.id.banner2)
    BannerLayout banner2;
    Unbinder unbinder;

    @BindView(R.id.text_qiandao)
    TextView textQiandao;
    @BindView(R.id.layout_qiandao)
    LinearLayout layoutQiandao;
    @BindView(R.id.layout_notice)
    LinearLayout layoutNotice;
    @BindView(R.id.layout_fenxiao)
    LinearLayout layoutFenxiao;
    @BindView(R.id.layout_other)
    LinearLayout layoutOther;
    @BindView(R.id.imageView3)
    ImageView imageView3;
    @BindView(R.id.marqueeView)
    MarqueeView marqueeView;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
 /*   @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;*/

    List<String> mDatas;

    IndexAdapter mIndexAdapter;
    List<String> urls = new ArrayList<>();

    List<IndexBean.DataBean.BannerBean> bannerBeen;

    //    List<IndexBean.DataBean.GoodstypeBean> goodstypeBeen;
    List<IndexBean.DataBean.InfoBean> infoBeen;
    String notice_id;
    String notice_title;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //  return super.onCreateView(inflater, container, savedInstanceState);
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_all, null);

        unbinder = ButterKnife.bind(this, view);

        bannerBeen = new ArrayList<>();
        //   goodstypeBeen = new ArrayList<>();
        infoBeen = new ArrayList<>();
        getBannerData();


        recyclerView.setLayoutManager(new FullyGridLayoutManager(getActivity(), 2));
        mDatas = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            mDatas.add("屌人" + i);
        }
        //   mIndexAdapter = new IndexAdapter(mDatas);
        Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.line_divider_index);
        recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getContext(), drawable, 2));
        recyclerView.setAdapter(mIndexAdapter);
        recyclerView.setNestedScrollingEnabled(false);


/*        mIndexAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {

            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
             //   Toast.makeText(getActivity(), "点击了" + position, Toast.LENGTH_SHORT).show();
                new ConnAddShop(12+"","2", "1", "1", "1", new AsyCallBack<CommenBean>() {

                    @Override
                    public void onStart(int type) throws Exception {
                        super.onStart(type);
                    }

                    @Override
                    public void onSuccess(String toast, int type, Object o, CommenBean commenBean) throws Exception {
                        super.onSuccess(toast, type, o, commenBean);
                        Toast.makeText(getActivity(), "成功加入购物车", Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onFail(String toast, int type) throws Exception {
                        super.onFail(toast, type);
                    }

                    @Override
                    public void onEnd(String toast, int type) throws Exception {
                        super.onEnd(toast, type);
                    }
                }

                ).execute(getActivity(), false);

            }
        });*/


        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick({R.id.text_qiandao, R.id.layout_qiandao, R.id.layout_notice, R.id.layout_fenxiao, R.id.layout_other, R.id.imageView3, R.id.marqueeView})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.text_qiandao:
                break;
            case R.id.layout_qiandao:
                break;
            case R.id.layout_notice:
                Intent intent = new Intent();

                intent.putExtra("id", notice_id);
                intent.putExtra("title", notice_title);
                intent.setClass(getActivity(), NoticeActivity.class);
                startActivity(intent);

                break;
            case R.id.layout_fenxiao:
                Intent intent2 = new Intent();

                intent2.putExtra("id", notice_id);
                intent2.putExtra("title", notice_title);
                intent2.setClass(getActivity(), NoticeActivity.class);
                startActivity(intent2);
                break;
            case R.id.layout_other:
                break;
            case R.id.imageView3:
                break;
            case R.id.marqueeView:
                break;

        }
    }


    public void getBannerData() {
        new ConnIndex("8","2",new AsyCallBack<IndexBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, final IndexBean indexBean) throws Exception {
                super.onSuccess(toast, type, o, indexBean);
                bannerBeen = indexBean.getData().getBanner();

                for (int i = 0; i < bannerBeen.size(); i++) {
                    urls.add(Conn.HOST3 + bannerBeen.get(i).getPicurl());

                }

                banner2.setViewUrls(urls);
                banner2.setOnBannerItemClickListener(new BannerLayout.OnBannerItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        // Toast.makeText(getActivity(), String.valueOf(position), Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent();

                        intent.putExtra("url", bannerBeen.get(position).getWeburl());
                        intent.setClass(getActivity(), IndexWebActivity.class);
                        startActivity(intent);
                    }
                });
                List<String> info = new ArrayList<>();
               /* goodstypeBeen = indexBean.getData().getGoodstype();
                for (int i = 0; i < goodstypeBeen.size(); i++) {
                    info.add(goodstypeBeen.get(i).getTitle());
                }*/
                infoBeen = indexBean.getData().getInfo();

                for (int i = 0; i < infoBeen.size(); i++) {
                    notice_id = infoBeen.get(i).getId() + "";
                    notice_title = infoBeen.get(i).getTitle();
                }


                marqueeView.startWithList(info);
                marqueeView.setOnItemClickListener(new MarqueeView.OnItemClickListener() {
                    @Override
                    public void onItemClick(int position, TextView textView) {
                        position = position + 1;
                        Toast.makeText(getActivity(), "点击了" + position + "购物车", Toast.LENGTH_SHORT).show();
                    }
                });


            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }
        }


        ).execute(getActivity(), false);
    }


}
